﻿

local moduleName = "Name"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Unit Name",

	["Status colored Name"] = true,
	["Custom"] = true,
	["Text Format"] = true,

	["Predefined Formats"] = true,
	["Choose from a set of prefedined formats."] = true,

	["Show inside"] = true,
	["Show the Name inside the bar."] = true,

	["Font Size"] = true,
	["Change font size"] = true,

	["Fonts"] = true,
	["Change font"] = true,

	["Width"] = true,
	["Change Width"] = true,
}
if GetLocale() == "zhCN" then
	L[moduleName] = "角色名"

	L["Status colored Name"] = "彩色状态名"
	L["Custom"] = "自定义"
	L["Text Format"] = "文本格式"

	L["Predefined Formats"] = "预置格式"
	L["Choose from a set of prefedined formats."] = "选择一个预置的格式"

	L["Show inside"] = "显示在生命条内"
	L["Show the Name inside the bar."] = "将名字显示在生命条内，否则显示在生命条外"

	L["Font Size"] = "字体大小"
	L["Change font size"] = "改变字体大小"

	L["Fonts"] = "字体"
	L["Change font"] = "选择字体"

	L["Width"] = "宽"
	L["Change Width"] = "显示宽度"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "角色名"

	L["Status colored Name"] = "彩色狀態名"
	L["Custom"] = "自訂"
	L["Text Format"] = "文本格式"

	L["Predefined Formats"] = "預置格式"
	L["Choose from a set of prefedined formats."] = "選擇一個預置的格式"

	L["Show inside"] = "顯示在生命條內"
	L["Show the Name inside the bar."] = "將名字顯示在生命條內，否則顯示在生命條外"

	L["Font Size"] = "字體大小"
	L["Change font size"] = "改變字體大小"

	L["Fonts"] = "字體"
	L["Change font"] = "選擇字體"

	L["Width"] = "寬"
	L["Change Width"] = "顯示寬度"
end

----------------------------------
--      Local Declaration      --
----------------------------------

local DogTag = LibStub("LibDogTag-3.0")
LibStub("LibDogTag-Unit-3.0")
local SharedMedia     = LibStub("LibSharedMedia-3.0")
local SharedMediaTypeFont = SharedMedia.MediaType and SharedMedia.MediaType.FONT or "font"
local fbl = SharedMedia:List(SharedMediaTypeFont)

local formats = {
	["[RaidGroup:Angle] [Name:ClassColor]"] = L["Status colored Name"],
	["NONE"] = L["Custom"]
}


----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		format = {
			type  = "input",
			name  = L["Text Format"],
			desc  = L["Text Format"],
			usage = "LibDogTag-3.0 tags. See http://www.wowace.com/wiki/LibDogTag-3.0/Tags",
			get   = "GetSetFormat",
			set   = "GetSetFormat"
		},
		preformats = {
			type  = "select",
			name  = L["Predefined Formats"],
			desc  = L["Choose from a set of prefedined formats."],
			values     = formats,
			get = "GetSetPreformats",
			set = "GetSetPreformats"
		},
--[[		fontsize = {
			type = "range",
			name = L["Font Size"],
			desc = L["Change font size"],
			min = 6,
			max = 30,
			step = 1,
			get  = "GetSetFontSize",
			set  = "GetSetFontSize"
		},
		fonts = {
			type = "select",
			name = L["Fonts"],
			desc = L["Change font"],
			get  = "GetSetFonts",
			set  = "GetSetFonts",
			values = fbl
		},
		width = {
			type = "range",
			name = L["Width"],
			desc = L["Change Width"],
			min = 20,
			max = 200,
			step = 5,
			get  = "GetSetWidth",
			set  = "GetSetWidth"
		} ]]
	}
}

plugin.defaultDB = {
	profile = {
		format = "[RaidGroup] [Name:ClassColor]",
		fonts = {
			size = 12,
			font = "",
			justifyH = "LEFT",
			justifyV = "TOP",
		},
		style = {
			position = "TOPLEFT",
			strata = 5,
			xof = 2,
			yof = -2,
			width = 0,
			height = 0,
		}
	}
}

----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	-- stupix fix for removing the "Dead" of just resurrected people, only works if they recieve a buff that boosts their hp
	-- why is there no event for this?
	--self:RegisterBucketEvent("UNIT_MAXHEALTH", 2, "UpdateUnits")
end


function plugin:OnCreate(frame)
	local font = CreateFrame("Frame", nil, frame)
	font:Show()

	local text = font:CreateFontString(nil, "OVERLAY")
	text:SetFont(self.db.profile.fonts.font, self.db.profile.fonts.size)
	text:SetJustifyH(self.db.profile.fonts.justifyH)
	text:SetJustifyV(self.db.profile.fonts.justifyV)
	text:SetShadowColor(0, 0, 0, 1)
	text:SetShadowOffset(0.8, -0.8)
	text:SetAllPoints(font)
	text:Show()
	font.text = text

	return font
end


function plugin:OnUpdate(frame)
	DogTag:AddFontString(frame.parts[moduleName].text, frame, self.db.profile.format, "Unit", { unit = frame.unit })
end


function plugin:OnInactive(frame)
	DogTag:RemoveFontString(frame.parts[moduleName].text)
end



----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetFormat(_, v)
	if type(v) == "nil" then return self.db.profile.format end

	-- this check is to assure we don't update to often
	if self.db.profile.format ~= v then
		self.db.profile.format = v --DogTag:FixCasing(v)

		self:UpdateAll()
		self:GetSetPreformats()
	end
end

function plugin:GetSetFonts(_, v)
	if type(v) == "nil" then
		for i, k in pairs(fbl) do
			if k == self.db.profile.fonts then
				return i
			end
		end
	end

	if self.db.profile.fonts ~= fbl[v] then
		self.db.profile.fonts = fbl[v]

		self:UpdateAllWith(function(frame)
			frame.parts[moduleName]:SetFont(SharedMedia:Fetch(SharedMediaTypeFont, self.db.profile.fonts), self.db.profile.fontsize)
		end)
	end
end

function plugin:GetSetFontSize(_, v)
	if type(v) == "nil" then return self.db.profile.fontsize end

	if self.db.profile.fontsize ~= v then
		self.db.profile.fontsize = v

		self:UpdateAllWith(function(frame)
			frame.parts[moduleName]:SetFont(SharedMedia:Fetch(SharedMediaTypeFont, self.db.profile.fonts), self.db.profile.fontsize)
		end)
	end
	self:UpdateAll()
end
function plugin:GetSetPreformats(_, v)
	if type(v) == "nil" then
		if formats[self.db.profile.format] then
			return self.db.profile.format
		else
			return "NONE"
		end
	end
	if v ~= "NONE" then
		self:GetSetFormat(_, v)
	else
		self:GetSetFormat(_, "")
	end
end

function plugin:GetSetWidth(_, v)
	if type(v) == "nil" then return self.db.profile.width end

	if self.db.profile.width ~= v then
		self.db.profile.width = v

		self:UpdateAll()
	end

end
function plugin:SetFont()
	self:UpdateAllWith(function(frame)
		frame.parts[moduleName].text:SetFont(self.db.profile.fonts.font, self.db.profile.fonts.size)
		frame.parts[moduleName].text:SetJustifyH(self.db.profile.fonts.justifyH)
		frame.parts[moduleName].text:SetJustifyV(self.db.profile.fonts.justifyV)

	end)
end